package shuashuami.hb.com.avtivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.http.HttpMethods;
import shuashuami.hb.com.http.HttpOnNextListener;
import shuashuami.hb.com.http.UploadErrorHttp;
import shuashuami.hb.com.manager.HbAppManager;
import shuashuami.hb.com.util.SystemUtil;

public class HbRegisteredAct extends AbActivity {

    private Button btnNext;
    private EditText editUserName;
    private EditText editPwd;
    private EditText editCode;

    private TextView txtGetCode;
    private TextView tvCountDown;

    private int recLen = 60;//倒计时秒数

    private boolean isGetCode = true;

    @Override
    protected void initUI() {
        setAbContentView(R.layout.activity_hb_registered);
        btnNext = (Button) findViewById(R.id.btn_registered_next);
        editUserName = (EditText) findViewById(R.id.register_edit_username);
        editPwd = (EditText) findViewById(R.id.register_edit_pwd);
        editCode = (EditText) findViewById(R.id.register_edit_code);
        txtGetCode = (TextView) findViewById(R.id.register_txt_getcode);
        tvCountDown = (TextView) findViewById(R.id.tv_hb_registered_count_down);
    }

    @Override
    protected void initData() {
        setTitleBar("注册(1/2)");
        setLeftView();
    }

    @Override
    protected void initListener() {
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                register();
                //  upload();
            }
        });
        txtGetCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isGetCode) {
                    getCode();
                } else {
                    ToastUtil.showShort(HbRegisteredAct.this, "请勿重复点击");
                }
            }
        });
        setLeftViewListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public void getCode() {
        isGetCode = false;
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                //    openActivityAndCloseThis(HbRegistered2Act.class);
                Message message = handler.obtainMessage(2);
                // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };

        HttpMethods.getCode(listener,
                editUserName.getText().toString(), "register"
        );
    }

    private void countDown() {
        tvCountDown.setVisibility(View.VISIBLE);
        Message message = handler.obtainMessage(1);     // Message
        handler.sendMessageDelayed(message, 1000);

    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {
                case 1:
                    recLen--;
                    tvCountDown.setText("" + recLen);

                    if (recLen > 0) {
                        Message message = handler.obtainMessage(1);
                        handler.sendMessageDelayed(message, 1000);      // send message
                    } else {
                        isGetCode = true;
                        recLen = 60;
                        tvCountDown.setVisibility(View.GONE);
                    }
                    break;
                case 2:

                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 200) {
                            ToastUtil.showShort(HbRegisteredAct.this, "获取成功");
                            countDown();
                        } else {
                            isGetCode = true;
                            ToastUtil.showShort(HbRegisteredAct.this, jsonObject.getString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;

                case 3:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 200) {
                            Intent intent = new Intent(HbRegisteredAct.this, HbRegistered2Act.class);
                            Bundle bundle = new Bundle();
                            bundle.putString("phone", editUserName.getText().toString());
                            bundle.putString("password", editPwd.getText().toString());
                            intent.putExtras(bundle);
                            startActivity(intent);
                            finish();
                        } else if (jsonObject.getString("message").equals("手机号不正确")) {
                            upload();
                        } else {
                            ToastUtil.showShort(HbRegisteredAct.this, jsonObject.getString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }

            super.handleMessage(msg);
        }
    };

    private void upload() {
        UploadErrorHttp uploadErrorHttp = new UploadErrorHttp(this);
        uploadErrorHttp.upload(String.format(String.format("手机号：" + editUserName.getText().toString()
                + "密码：" + editPwd.getText().toString() + "验证码" + editCode.getText().toString() +
                "手机厂商：" + SystemUtil.getDeviceBrand() + "手机型号：" + SystemUtil.getSystemModel() +
                "Android系统版本号：" + SystemUtil.getSystemVersion() + "cookie：" + HbAppManager.getClearableCookieJar())));

    }

    public void register() {
        if (!checkValid()) return;

        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                //   openActivityAndCloseThis(HbRegistered2Act.class);
                Message message = handler.obtainMessage(3);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);
            }
        };

        HttpMethods.register1(listener,
                editUserName.getText().toString(),
                editCode.getText().toString(),
                editPwd.getText().toString()
        );
    }

    public boolean checkValid() {
        // 验证用户名是否合法
        if (editUserName.getText().toString().equals("")) {
            return false;
        }
        if (editCode.getText().toString().equals("")) {
            return false;
        }

        if (editPwd.getText().toString().equals("")) {
            return false;
        }
        return true;
    }
}
